/**
 * Created by saber on 15/8/28.
 */

wcApp.controller('VerifyCtrl', ['$scope', '$state', 'ionicMaterialInk', 'ionicMaterialMotion','$http','$timeout','user','$location','$rootScope',
    function ($scope, $state, ionicMaterialInk, ionicMaterialMotion,$http,$timeout,user,$location,$rootScope) {

        var checkLogin=function(){
            if (user.hasLogin()){
                $state.go("app.depsList");
            }else{
                if (!user.done()){
                    $state.go("app.main");
                }

            }
        }

        checkLogin();

        $rootScope.$on("$locationChangeStart", function (e, n, o, ns, os) {
            if ($location.path()=='/verify'){
                checkLogin();
            };
        })




        ionicMaterialInk.displayEffect();

        $scope.subTitle = '验证手机号';

        $scope.hasVerified = false;

        $scope.buttonSMSText = "发送短信验证码";

        $scope.verifyFields={};

        $scope.smsResendFlag=true;

        $scope.ctrl={
            disable:false
        }

        $scope.psnInfoFields={
            sex:"male",
            type:"league"
        };

        $scope.smsMsg="";

        $scope.msg={
            show:false,
            err:false,
            content:""
        };


        $scope.showTips=function(cnt,err,time){
            $scope.msg.content=cnt;
            $scope.msg.err=err;
            $scope.msg.f=true;
            console.log($scope.msg);
            if (time){
                $timeout(function () {
                    $scope.msg.f=false;
                },time);
            }

        };
        var showTips=$scope.showTips;


        function smsResendCountdown(time){
            $scope.smsCountdown=$timeout(function(){
                if (time != 0 ){
                    $scope.buttonSMSText="还有"+time+"秒可以重发短信";
                    smsResendCountdown(--time);
                }else{
                    $scope.smsResendFlag=true;
                    $scope.buttonSMSText = "重新发送短信验证码";
                }
            },1000)
        }

        $scope.Verify = function (flag) {

            $scope.ctrl.disable=true;

            if (flag){
                //流程已完成
                $http.post($rootScope.baseUrl+"/users/verifyCode",{code: $scope.verifyFields.code})
                    .success(function(resp,code,data) {
                        $scope.ctrl.disable=false;
                        if (resp.code !=200){
                            showTips(resp.msg,true,5000);
                        }else{
                            $timeout.cancel($scope.smsCountdown);
                            showTips("_(:з」∠)_",false,1);
                            $scope.subTitle="身份信息录入";
                            $scope.hasVerified=true;
                        }
                    });
            }else{
                //发送验证码
                $http.post($rootScope.baseUrl+"/users/sendSMS",{cap:$scope.verifyFields.cap,mobile:$scope.verifyFields.phone})
                    .success(function(resp,code,data){
                        $scope.ctrl.disable=false;
                        if (resp.code !=200){
                            showTips(resp.msg,true,5000);
                        }else{
                            showTips(resp.msg,false,5000);
                            $scope.SMSSent = true;
                            $scope.buttonSMSText = "重新发送短信验证码";
                            $scope.smsResendFlag=false;
                            smsResendCountdown(60);
                        }


                    })
            }







        };

        $scope.create= function () {
            $scope.ctrl.disable=true;
            $http.post($rootScope.baseUrl+"/users/create",{
                realName:$scope.psnInfoFields.realName,
                stdNumber:$scope.psnInfoFields.stdNumber,
                mobile:$scope.verifyFields.phone,
                type:$scope.psnInfoFields.type,
                sex:$scope.psnInfoFields.sex
            })
                .success(function(resp,code,data){

                    if (resp.code !=200){
                        showTips(resp.msg,true,5000);
                        $scope.ctrl.disable=false;
                    }else{
                        user.setUser(resp.user);
                        console.log(resp);
                        $http.get($rootScope.baseUrl+"/enroll/ifICan")
                            .success(function(resp,code,data){
                                if (resp.code==200){
                                    $state.go('app.depsList');

                                }else if (resp.code==204){
                                    $http.get($rootScope.baseUrl+'/enroll/getRequest')
                                        .success(function (resp, code) {
                                            if (code==200&&resp.code==200&&resp.request.length!=0){
                                                request.describe=resp.request.describe;
                                                request.firstWish=resp.request.firstWish;
                                                request.secondWish=resp.request.secondWish;
                                                $state.go("app.myRequest");
                                            }else{
                                                console.log(resp,code);
                                            }
                                        });
                                }else if(resp.code==208){
                                    $scope.ctrl.disable=true;
                                    showTips("对不起，活动已过期",true);
                                }
                            });
                    }


                })
        }

        $scope.capAddr = $rootScope.baseUrl+"/users/getCap";

        $scope.chgCap = function () {
            var seed = "01234567890abcdefghijklmnopqrstuvwxyz";
            var str = "";
            for (var i = 0; i < 6; i++)str += seed[Math.round(Math.random() * (seed.length - 1))];
            $scope.capAddr = $rootScope.baseUrl+"/users/getCap" + "?content=" + str;
        }
    }]);